$(function(){
	
	//添加商品
	var $btnAdd = $('#btnAdd');
	$btnAdd.click(function(){
		orderGoodsAdd();
	});
	
	$('#goods').change(function(){
		
		var goods_id = $(this).val();

		var timestamp=new Date().getTime();
		$('#p-attr').load('/index.php/Admin/Order/goods_attr?goods_id=' + goods_id + '&t=' + timestamp);
	});
	
	
});



//删除订单商品
function orderGoodsAdd(){
	
	var goods_id 	= parseInt($('#goods').val());
	var goods_name 	= $('#goods option:selected').text();
	var goods_attr 	= $('#attrs').val();
	var order_id	= $('#order_id').val();
	
	
	
	if(goods_id == 0){
		alert('请先选择商品');
		return false;
	}
	
	if(goods_attr == null){
		alert('没有找到商品属性,请先为商品添加属性');
		return false;
	}
	
	if(!confirm('确认要添加 ' + goods_name + ' 到订单?')){
		return false;
	}
	
	var params = {
		'order_id':order_id,
		'goods_id':goods_id,
		'goods_attr':goods_attr
	}
	
	$.post('/index.php/Admin/Order/order_goods_add',params,function(data){
		if(data.success){
			
			alert(data.message);
			
			var timestamp=new Date().getTime();
			$('#div-goods').load('/index.php/Admin/Order/goods_update_goods?order_id=' + order_id + '&t=' + timestamp);
		}else{
			alert(data.message);
		}
		
	},'json');
	
}


//删除订单商品
function orderGoodsDel(id){
	
	var order_id		= $('#order_id').val();
	
	if(!confirm('确认要删除?')){
		return false;
	}
	
	var params = {
		'id':id
	}
	
	$.post('/index.php/Admin/Order/order_goods_del',params,function(data){
		if(data.success){
			var timestamp=new Date().getTime();
			$('#div-goods').load('/index.php/Admin/Order/goods_update_goods?order_id=' + order_id + '&t=' + timestamp);
		}else{
			alert('删除失败');
		}
		
	},'json');
	
}

//更新商品数量
function orderGoodsUpdate(id){

	var $goods_number 	= $('#goods_number_' + id);
	var goods_number 	= parseInt($.trim($goods_number.val()));
	var order_id		= $('#order_id').val();
	
	if(isNaN(goods_number)){
		alert('请填写正确的数量');
		$goods_number.focus();
		return false;
	}
	
	var params = {
		'id':id,
		'goods_number':goods_number
	}
	
	$.post('/index.php/Admin/Order/order_goods_update',params,function(data){
		if(data.success){
			
			var timestamp=new Date().getTime();
			$('#div-goods').load('/index.php/Admin/Order/goods_update_goods?order_id=' + order_id + '&t=' + timestamp);
			
		}else{
			alert('数量更新失败');
		}
		
	},'json');
	
}